package com.red.star.wechat.data.mappers;

import com.red.star.wechat.data.core.base.MyMapper;
import com.red.star.wechat.data.entity.FansCard;
import com.red.star.wechat.data.entity.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @author AMGuo
 * @Description 粉丝蓄客卡
 * @date 2018/03/02 10:48
 */
public interface FansCardMapper extends MyMapper<FansCard> {

    public FansCard findByCondition(@Param("cardId") Integer cardId, @Param("groupId") Integer groupId,
                                    @Param("employeeId") Integer employeeId, @Param("unionId") String unionId);

    @Select(value = "select * from t_fans_card t where t.union_id is not null")
    public List<FansCard> findByNotNullUnionId();

    @Select(value = "select count(t.card_id) from t_fans_card t where t.card_id = #{cardId} and" +
            " t.pay_status = #{status} and t.union_id = #{unionId}")
    public Integer findByStatus(@Param("cardId") Integer cardId, @Param("status") Integer status,
                                @Param("unionId") String unionId);

    Integer countByOpenId(@Param("openId") String openId, @Param("nowDate") Date nowDate,
                          @Param("omsCode") String omsCode);

    List<FansCard> findByOpenId(@Param("openId") String openId, @Param("nowDate") Date nowDate,
                                @Param("omsCode") String omsCode);

    public List<FansCardPojo> getAllFansCardList(FansCardPojo fansCardPojo);

    public List<FansCard> queryStoredCardByFansCard(@Param("id") Integer id);

    @Select(value = "select count(t.card_id) from t_fans_card t where t.card_id = #{cardId} and" +
            " t.open_id = #{openId}")
    Integer countByCardIdAOpenId(@Param("cardId") Integer cardId, @Param("openId") String openId);

    @Select(value = "select * from t_fans_card t where t.card_id = #{cardId} and" +
            " t.open_id = #{openId} limit 1")
    FansCard findByCardIdAOpenId(@Param("cardId") Integer cardId, @Param("openId") String openId);

    @Select(value = "select count(t.card_id) from t_fans_card t where t.card_id = #{cardId} " +
            "and t.open_id = #{openId} and t.pay_status = 1")
    Integer countEffectiveByCardIdAOpenId(@Param("cardId") Integer cardId, @Param("openId") String openId);

    @Select(value = "select * from t_fans_card t where t.order_id = #{orderId} and t.pay_status = 0")
    FansCard findByOrderId(@Param("orderId") String orderId);

    @Update(value = "update t_fans_card set pay_status = #{payStatus} where order_id = #{orderId}")
    Integer updateStatusByOrderId(@Param("orderId") String orderId, @Param("payStatus") Integer payStatus);

    @Select(value = "select * from t_fans_card t where t.order_id = #{orderId} and t.pay_status = #{payStatus}")
    FansCard findByOrderIdAStatus(@Param("orderId") String orderId, @Param("payStatus") Integer status);

    List<FansCard> listSoldStoredCard(@Param("employeeId") Integer employeeId,
                                      @Param("activityId") Integer activityId,
                                      @Param("groupIds") Integer[] groupIds,
                                      @Param("storedCardIds") Integer[] storedCardIds,
                                      @Param("startTime") Date startTime,
                                      @Param("endTime") Date endTime);

    /**
     * 导购所在联盟组售卡统计
     *
     * @param employeeId    导购ID
     * @param activityId    活动ID
     * @param groupIds      联盟组ID
     * @param storedCardIds 蓄客卡ID
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @return
     */
    List<FansCard> listGroupSoldStoredCard(@Param("employeeId") Integer employeeId,
                                           @Param("activityId") Integer activityId,
                                           @Param("groupIds") Integer[] groupIds,
                                           @Param("storedCardIds") Integer[] storedCardIds,
                                           @Param("startTime") Date startTime,
                                           @Param("endTime") Date endTime);

    List<UnionSoldCard> listUnionSoldCard(@Param("mallCode") String mallCode,
                                          @Param("activityId") Integer activityId,
                                          @Param("groupIds") Integer[] groupIds,
                                          @Param("startTime") Date startTime,
                                          @Param("endTime") Date endTime,
                                          @Param("excitationType") Integer excitationType,
                                          @Param("cardIds") Integer[] cardIds);

    List<EmployeeSoldCard> listEmployeeSoldCard(@Param("mallCode") String mallCode,
                                                @Param("activityId") Integer activityId,
                                                @Param("groupIds") Integer[] groupIds,
                                                @Param("storedCardIds") Integer[] storedCardIds,
                                                @Param("startTime") Date startTime,
                                                @Param("endTime") Date endTime);

    List<EmployeeSoldCardDetail> listEmployeeSoldCardDetail(@Param("employeeId") Integer employeeId,
                                                            @Param("activityId") Integer activityId,
                                                            @Param("groupIds") Integer[] groupIds,
                                                            @Param("storedCardIds") Integer[] storedCardIds,
                                                            @Param("startTime") Date startTime,
                                                            @Param("endTime") Date endTime);

    List<StoredCardOrderDetail> listStoredCardOrderDetail(StoredCardOrderDetail storedCardOrderDetail);

    List<EmployeeSoldCard> listShopOwnerSoldCard(@Param("merchantId") Integer merchantId,
                                                 @Param("activityId") Integer activityId,
                                                 @Param("storedCardIds") Integer[] storedCardIds,
                                                 @Param("startTime") Date startTime,
                                                 @Param("endTime") Date endTime);

    List<FansCard> findFansCardByCardId(@Param("cardId") Integer cardId);

    /**
     * 根据活动id查询本活动下卖出卡数
     *
     * @param id
     * @return
     */
    @Select("select count(t.id) from t_fans_card t left join t_stored_card tsc on tsc.id = t.card_id where t.pay_status != 0 and tsc.activity_id = #{id}")
    Integer countByActivityId(@Param("id") Integer id);

    int countByGroupId(@Param("groupId") Integer groupId);

}
